<template>
  <div class="subcontract-registration-manage">
    <div class="model-title-css">
      <div>设计变更登记</div>
    </div>
    <div class="background_wrap public-flex-row">
      <div class="left-box WholeScroll">
        <el-tree class="Public-Tree-Class" :data="leftList" :props="defaultProps" ref='treeData' node-key="id" :highlight-current='true' :default-expanded-keys="defaultTreeKey" @node-click="handleNodeClick"></el-tree>
      </div>
      <div class="right-box" style="padding-right: 15px">
        <div class="operate_wrap flex">
          <div class="flex-center">
            <el-button type="primary" size="small" class="add buildButtonStyle" v-permission="permission.add" round @click="showDialog('build', '新增')"><i class="iconfont icon-icon_add"></i> 新增
            </el-button>

          </div>
          <div class="flex-center g-flex flex-right">
            <el-select class="select" size="small" v-model="search.changeLevel" placeholder="选择变更等级" v-on:change="getList()" clearable>
              <el-option v-for="item in changeLevelArr" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>

            <el-date-picker size="small" class="datePicker" v-model="search.proposeDate" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd" clearable placeholder="请选择填报时间" @change="getList(true)">
            </el-date-picker>

            <!-- <el-date-picker size="small" style="width: 200px;margin-left: 15px" v-model="search.auditFinishDate" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd" clearable placeholder="请选择完成日期">
            </el-date-picker> -->

            <el-input class="input" size="small" v-model="search.changeCode" placeholder="请输入变更编号" clearable @change="getList(true)"></el-input>

            <el-button class="search" size="small" @click="getList(true)">
              <i class="iconfont icon-sousuo"></i>&nbsp;搜索
            </el-button>
          </div>
        </div>
        <div class="list_wrap WholeScroll">
          <el-table class="table-three-blue " :border="true" :row-class-name="tableRowStyleName" :cell-class-name="tableCellStyleName" :header-cell-class-name="tableHeaderCellStyleName" :stripe="true" ref="multipleTable" :data="list" tooltip-effect="dark" @cell-click="cellClick" style="width: 100%; font-family: PingFangSC-Regular; font-size: 14px">

            <el-table-column show-overflow-tooltip prop="xh" align="center" label="序号" width="60">
              <template slot-scope="scope">
                {{ scope.$index + search.pageNo * search.pageSize + 1 }}
              </template>
            </el-table-column>
            <el-table-column show-overflow-tooltip prop="sectionName" label="标段" width="120">
              <template slot-scope="scope">
                {{scope.row.sectionName}}
              </template>
            </el-table-column>

            <el-table-column show-overflow-tooltip label="变更编号" prop="changeCode" width="160">
              <template slot-scope="scope">
                <div>{{scope.row.changeCode}}</div>
              </template>
            </el-table-column>
            <el-table-column show-overflow-tooltip label="变更名称" prop="changeName" width="450">
              <template slot-scope="scope">
                <div>{{scope.row.changeName}}</div>
              </template>
            </el-table-column>
            <el-table-column show-overflow-tooltip label="变更等级" prop="changeLevelName" width="80">
              <template slot-scope="scope">
                <div>{{scope.row.changeLevelName}}</div>
              </template>
            </el-table-column>
            <el-table-column show-overflow-tooltip label="变更增减金额(万元)" prop="changeAmount" width="160">
              <template slot-scope="scope">
                <div style="text-align: right;">{{scope.row.changeAmount}}</div>
              </template>
            </el-table-column>
            <!-- <el-table-column show-overflow-tooltip label="提出时间" prop="proposeDate" width="120">
              <template slot-scope="scope">
                <div>{{scope.row.proposeDate}}</div>
              </template>
            </el-table-column> -->
            <el-table-column show-overflow-tooltip label="填报人" prop="createrName" width="120">
              <template slot-scope="scope">
                <div>{{scope.row.createrName}}</div>
              </template>
            </el-table-column>
            <el-table-column show-overflow-tooltip label="填报时间" prop="createTime" width="170">
              <template slot-scope="scope">
                <div>{{scope.row.createTime}}</div>
              </template>
            </el-table-column>

            <el-table-column show-overflow-tooltip fixed="right" width="300px" label="操作">
              <template slot-scope="scope">
                <el-button size="mini" class="menu_icon iconfont icon-icon_look action_color1" v-permission="permission.show" @click="detail(scope.row)">&nbsp;查看</el-button>
                <el-button size="mini" class="menu_icon iconfont icon-xiugai-copy action_color1" v-permission="permission.edit" @click="edit(scope.row)">&nbsp;修改</el-button>
                <el-button size="mini" class="menu_icon iconfont icon-shanchu action_color2" v-permission="permission.del" @click="del(scope.row)">&nbsp;删除</el-button>
                <!-- <work-flow-btn :row="scope.row" :type="2" @edit="edit" @del="del" @approve="approve" @detail="detail" /> -->
              </template>
            </el-table-column>
          </el-table>
          <el-pagination style="float: right; margin-top: 30px" layout="prev, pager, next" :page-size="search.pageSize" :total="search.total" :current-page="search.pageNo + 1" :prev-text="tablePaginationPrev" :next-text="tablePaginationNext" @current-change="paginationChange">
          </el-pagination>
        </div>
      </div>
    </div>

    <Detail :postDetail="postDetailData" @reloadList="getList(true)"></Detail>

  </div>
</template>

<script>
import {
  tableRowStyleGol,
  tableCellStyleGol,
  tableHeaderCellStyleGol,
  doCreateObject
} from '@/utils/publicFun'
import msgUtil from '@/utils/msgUtil'
import projectSectionInfo from '@/api/buss/common'
import crudDict from '@/api/system/dict'
import designChangeApply from '@/api/buss/designChangeApply'
import WorkFlowBtn from '@/components/WorkFlow/table'
import publicSearchData from '@/mixins/publicSearchData'
import { genCbhtUserList } from "@/api/buss/designChangeApply";
export default {
  mixins: [publicSearchData],
  name: `demo`,
  components: {
    WorkFlowBtn,
    Detail: require(`./designChangeApply-detail`).default,
  },
  data() {
    return {
      changeLevelArr: [{
        name: '一般',
        id: '1'
      }, {
        name: '较大',
        id: '2'
      }, {
        name: '重大',
        id: '3'
      }],

      typeOptions: [
        {
          label: '合同分类',
          value: '',
        },
      ],
      leftList: [],
      defaultTreeKey: [],
      processDialog: false,
      defaultProps: {
        children: 'children',
        label: 'name',
      },
      chooseTreeData: {},
      permission: {
        add: ['100', '1009114:add'],
        show: ['100', '1009114:show'],
        edit: ['100', '1009114:edit'],
        del: ['100', '1009114:del'],
      },

      tableRowStyleName: tableRowStyleGol,
      tableCellStyleName: tableCellStyleGol,
      tableHeaderCellStyleName: tableHeaderCellStyleGol,
      tablePaginationPrev: '上一页',
      tablePaginationNext: '下一页',
      hasAdd: false, //是否有新增权限
      loading: false,
      //  当前查看的文件列表
      nowFileList: [],
      showFileDialog: false,
      list: [{}, {}, {}, {}],
      title: '人员',

      addSectionId: '',
      // 搜索参数
      search: {
        //pageIndex: 0,
        pageNo: 0,
        pageSize: 10,
        //pageSum: 0,
        total: 0,
        sectionId: '',
        projectId: '',
        changeCode: '',
        contractType: '',
        changeLevel: '',
        proposeDate: '',
        auditFinishDate: '',
      },
      // 向详情子组件的传值
      postDetailData: {
        show: false,
        type: 'build',
        typeName: '新增',
      },
      // 向详情子组件的传值
      treeEditDialogData: {
        show: false,
        nowTreeData: []
      },
    }
  },
  mounted() {
    this.initData()
    this.getContractArr()
  },
  methods: {
    checkFile(row) {
      console.log(row)
      if (row.hasOwnProperty('fileList') && row.fileList.length > 0) {
        let id = row.fileList[0].id
        this.$publicUpload.preView(id)
      } else {
        this.$messageWarning('暂无文件')
      }
    },
    approve(obj) {
      this.postDetailData = {
        show: true,
        type: 'approve',
        typeName: '审核',
        detailId: obj.id,
      }
    },
    edit(obj) {
      this.postDetailData = {
        show: true,
        type: 'edit',
        typeName: '编辑',
        detailId: obj.id,
      }
    },
    detail(obj) {
      this.postDetailData = {
        show: true,
        type: 'check',
        typeName: '查看',
        detailId: obj.id,
      }
    },
    del(obj) {
      if (obj.id === '') {
        msgUtil.error2('请先选择要删除的记录！')
        return
      }
      msgUtil.confirm('确认删除该条数据?').then(res => {
        let idlist = []
        idlist.push(obj.id)
        let param = {
          ids: idlist
        }
        designChangeApply.del(param).then(res => {
          if (res.code === 200) {
            msgUtil.success2('删除成功!')
            this.getList()
          } else {
            msgUtil.error2(res.data.message)
          }
        })
      })
    },


    getContractArr() {
      this.typeOptions = []
      crudDict.getByCode('contractClassA').then(res => {
        if (res.code === 200) {
          this.typeOptions = res.data;
          // for (var i in data) {
          //   this.typeOptions.push({'value':data[i].id,'label':data[i].name});
          // }
        } else {
          msgUtil.error2(res.data.message)
        }
      }).catch(() => {
        msgUtil.error2("获取问题归属失败")
      })
    },
    initData() {
      projectSectionInfo.getLeftProjectTree().then(res => {
        if (res.code === 200) {
          this.$set(this, 'leftList', res.data)

          if (this.leftList.length > 0) {
            this.$nextTick(() => {
              if (this.$headerSearch) {
                if (this.changeHeaderProject) {
                  this.$refs['treeData'].setCurrentKey(this.changeHeaderProject);
                  if (this.defaultTreeKey) {
                    this.defaultTreeKey.push(this.changeHeaderProject)
                  }
                  this.handleNodeClick({
                    id: this.changeHeaderProject,
                    type: 'projectId'
                  })
                } else {
                  this.defaultTreeKey.push(this.leftList[0].id)
                  this.$refs['treeData'].setCurrentKey(this.leftList[0].id);
                  if (this.defaultTreeKey) {
                    this.defaultTreeKey.push(this.leftList[0].id)
                  }
                  this.handleNodeClick(this.leftList[0])
                }
              } else {
                this.defaultTreeKey.push(this.leftList[0].id)
                this.$refs['treeData'].setCurrentKey(this.leftList[0].id);
                if (this.defaultTreeKey) {
                  this.defaultTreeKey.push(this.leftList[0].id)
                }
                this.handleNodeClick(this.leftList[0])
              }
            })
          } else {
            this.list = []
          }
        } else {
          this.$messageError(res.data.message)
        }
      }).catch(() => {
        this.$messageError('获取项目标段树失败')
      })
    },


    // 事件处理的api请求
    sealDealHttp(url, query, param, callback) {
      this.$httpPost(url + query, param)
        .then((res) => {
          if (res.status === 1) {
            if (callback) callback(res)
            // 重新获取数据
            setTimeout(() => {
              this.getList(true)
            }, 1000)
          } else {
            this.$messageError(res.message)
          }
        })
        .catch((res) => {
          console.log('请求出现错误', res)
          console.log(res)
        })
    },
    handleNodeClick(data) {
      this.search.projectId = '';
      this.search.sectionId = '';

      console.log(data);
      if (data.type == 'projectId') {
        this.search.projectId = data.id;
      }
      if (data.type == 'sectionId') {
        this.search.sectionId = data.id;
        this.addSectionId = data.id;
        this.search.projectId = data.projectId;
      }
      this.getList(true)
    },
    // 格式化表单数据
    formatter(row, column) {
      let array = column.property.split('.')
      let result
      array.forEach((item, i) => {
        result = result === undefined ? row[item] : result[item]
      })
      if (result === '' || result === undefined) {
        return '--'
      } else {
        return result
      }
    },
    // 点击单元格
    cellClick(row, column, cell, event) {
      if (column.property === 'changeCode') {
        this.detail(row)
      }
    },
    // 获取列表 api
    getList(reload) {
      if (reload) {
        this.search.pageNo = 0
      }
      this.list = []
      let param = {
        pageNo: this.search.pageNo,
        pageSize: this.search.pageSize,
        projectId: this.search.projectId,
        sectionId: this.search.sectionId,
        changeLevel: this.search.changeLevel,
        proposeDate: this.search.proposeDate,
        auditFinishDate: this.search.auditFinishDate,
        changeCode: this.search.changeCode,
      }
      designChangeApply.getTablePage(param).then(res => {
        if (res.code == 200) {
          this.list = res.data.content
          this.search.total = res.data.totalElements
        } else {
          msgUtil.error2(res.data.message)
        }
      })
    },

    // 切换列表
    paginationChange(index) {
      this.search.pageNo = index - 1
      this.getList()
    },
    /*  操作 */
    showDialog(type, typeName, detailId) {
      console.log(this.search.sectionId);
      if (!this.search.sectionId) {
        this.$messageWarning("请选择【标段】后再添加数据")
        return;
      }
      switch (type) {
        case 'build':
          this.postDetailData = {
            show: true,
            type: type,
            typeName: typeName,
            detailId: '',
            addSectionId: this.addSectionId,
            projectId: this.search.projectId,
            chooseTreeData: this.chooseTreeData
          }
          break
        case 'edit':
          this.postDetailData = {
            show: true,
            type: type,
            typeName: typeName,
            detailId: detailId,
            chooseTreeData: this.chooseTreeData
          }
          break
        case 'check':
          this.postDetailData = {
            show: true,
            type: type,
            typeName: typeName,
            detailId: detailId,
            chooseTreeData: this.chooseTreeData
          }
          break
        case 'delete':
          if (detailId === '') {
            msgUtil.error2('请先选择要删除的记录！')
            return
          }
          msgUtil.confirm('确认删除该条数据?').then(res => {
            let idlist = []
            idlist.push(detailId)
            let param = {
              ids: idlist
            }
            designChangeApply.del(param).then(res => {
              if (res.code === 200) {
                msgUtil.success2('删除成功!')
                this.getList()
              } else {
                msgUtil.error2(res.data.message)
              }
            })
          })
          break

      }
    },




  },
}
</script>

<style scoped lang="scss">
.model-title-css {
  display: flex;
  flex-direction: row;
  align-items: center;
}
.list_wrap {
  margin-top: 20px;
}
.left-box {
  min-width: 220px !important;
  max-width: 350px !important;
  max-height: calc(100vh - 200px);
  border-right: 1px #e2e8ec solid;
  overflow: auto;
  p {
    cursor: pointer;
    font-size: 14px;
  }
}
.right-box {
  width: calc(100% - 200px);
  padding-left: 15px;
  flex-grow: 1;
}
.operate_wrap {
  .select {
    width: 200px;
    margin-right: 15px;
  }
  .datePicker {
    width: 250px;
    margin-right: 15px;
  }
  .input {
    width: 200px;
    margin-right: 15px;
  }
  .search {
    width: 88px;
    margin-left: 0 !important;
  }
}
</style>
